package com.interest.plus2.view;

import com.interest.plus2.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;

/**
 * 首页底部buttom
 */
public class CustomMainTabButtom extends DirectionalLayout {
    private int[] imgSelectList = {ResourceTable.Media_icon_home_selected, ResourceTable.Media_icon_curriculum_selected,
            ResourceTable.Media_icon_studentshow_selected, ResourceTable.Media_icon_user_selected};
    private int[] imgNoSelectList = {ResourceTable.Media_icon_home_noselected, ResourceTable.Media_icon_curriculum_noselected,
            ResourceTable.Media_icon_studentshow_noselected, ResourceTable.Media_icon_user_noselected};

    /**
     * tab 选中监听
     */
    private TabChangeListener tabChangeListener;

    public void setTabChangeListener(TabChangeListener tabChangeListener) {
        this.tabChangeListener = tabChangeListener;
    }

    private String[] textList = {"口才课", "学习啦", "学员秀", "我的窝"};
    CustomMainTab tabHome;
    CustomMainTab Id_tabCulum;
    CustomMainTab Id_tabStudentShow;
    CustomMainTab Id_tabSetting;

    public CustomMainTabButtom(Context context) {
        super(context);
        initView(context);
    }

    public CustomMainTabButtom(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView(context);
    }

    public CustomMainTabButtom(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView(context);
    }

    private void initView(Context context) {
        LayoutScatter.getInstance(context).parse(ResourceTable.Layout_main_home_buttom, this, true);
        tabHome = (CustomMainTab) findComponentById(ResourceTable.Id_tabHome);
        Id_tabCulum = (CustomMainTab) findComponentById(ResourceTable.Id_tabCulum);
        Id_tabStudentShow = (CustomMainTab) findComponentById(ResourceTable.Id_tabStudentShow);
        Id_tabSetting = (CustomMainTab) findComponentById(ResourceTable.Id_tabSetting);

        tabHome.setClickedListener(component -> {
            setTabSelect(0);
            if (tabChangeListener != null) {
                tabChangeListener.onTabChangeListener(0);
            }
        });
        Id_tabCulum.setClickedListener(component -> {
            setTabSelect(1);
            if (tabChangeListener != null) {
                tabChangeListener.onTabChangeListener(1);
            }
        });
        Id_tabStudentShow.setClickedListener(component -> {
            setTabSelect(2);
            if (tabChangeListener != null) {
                tabChangeListener.onTabChangeListener(2);
            }
        });
        Id_tabSetting.setClickedListener(component -> {
            setTabSelect(3);
            if (tabChangeListener != null) {
                tabChangeListener.onTabChangeListener(3);
            }
        });

        tabHome.setText(textList[0]);
        Id_tabCulum.setText(textList[1]);
        Id_tabStudentShow.setText(textList[2]);
        Id_tabSetting.setText(textList[3]);


        //默认第一个选中
        tabHome.setIsSelect(true);
        tabHome.setRes(imgSelectList[0]);
        Id_tabCulum.setRes(imgNoSelectList[1]);
        Id_tabStudentShow.setRes(imgNoSelectList[2]);
        Id_tabSetting.setRes(imgNoSelectList[3]);
    }

    /**
     * 设置tab选中
     *
     * @param index tab索引
     */
    public void setTabSelect(int index) {
        switch (index) {
            case 0:
                tabHome.setIsSelect(true);
                Id_tabCulum.setIsSelect(false);
                Id_tabStudentShow.setIsSelect(false);
                Id_tabSetting.setIsSelect(false);

                tabHome.setRes(imgSelectList[0]);
                Id_tabCulum.setRes(imgNoSelectList[1]);
                Id_tabStudentShow.setRes(imgNoSelectList[2]);
                Id_tabSetting.setRes(imgNoSelectList[3]);
                break;
            case 1:
                tabHome.setIsSelect(false);
                Id_tabCulum.setIsSelect(true);
                Id_tabStudentShow.setIsSelect(false);
                Id_tabSetting.setIsSelect(false);

                tabHome.setRes(imgNoSelectList[0]);
                Id_tabCulum.setRes(imgSelectList[1]);
                Id_tabStudentShow.setRes(imgNoSelectList[2]);
                Id_tabSetting.setRes(imgNoSelectList[3]);
                break;
            case 2:
                tabHome.setIsSelect(false);
                Id_tabCulum.setIsSelect(false);
                Id_tabStudentShow.setIsSelect(true);
                Id_tabSetting.setIsSelect(false);

                tabHome.setRes(imgNoSelectList[0]);
                Id_tabCulum.setRes(imgNoSelectList[1]);
                Id_tabStudentShow.setRes(imgSelectList[2]);
                Id_tabSetting.setRes(imgNoSelectList[3]);
                break;
            case 3:
                tabHome.setIsSelect(false);
                Id_tabCulum.setIsSelect(false);
                Id_tabStudentShow.setIsSelect(false);
                Id_tabSetting.setIsSelect(true);

                tabHome.setRes(imgNoSelectList[0]);
                Id_tabCulum.setRes(imgNoSelectList[1]);
                Id_tabStudentShow.setRes(imgNoSelectList[2]);
                Id_tabSetting.setRes(imgSelectList[3]);
                break;

        }

    }

}
